﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Services;
using System.Web.Services.Protocols;
using UwaySoft.EsheetServer.BizLogic;
using UwaySoft.EsheetServer.Help;

namespace UwaySoft.EsheetServer.Service
{
    [WebService(Namespace = "http://132.121.31.2/esheetServer/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
    public class eSheetServer : WebService
    {
        private readonly WorkHepler _workHepler = new WorkHepler();

        /// <summary>
        /// 投诉派单
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        [WebMethod]
        public string NewSheet(string xmlstring)
        {
            try
            {
                string s = UnicodeToGB(xmlstring);
                xmlstring = s;
            }
            catch
            {
            }
            Log.Info("产生新投诉工单");
            Log.Debug("接收数据：\r\n" + xmlstring);

            FilterHtmlCharts(ref xmlstring);

            string msg = "";
            try
            {
                var response = _workHepler.NewSheet(xmlstring);
                msg = response.ToString();
            }
            catch (Exception ex)
            {
                Log.Debug("接收数据错误：" +ex.Message);
            }
            Log.Debug("接收投诉工单完成");
            return msg;

            //测试用
            //return new NewSheetResponse(false, "-1", "-1", xmlstring).ToString();
        }        

        /// <summary>
        /// 撤单/附加意见/催单
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        [WebMethod]
        public string FollowUpOper(string xmlstring)
        {
            try
            {
                string s = UnicodeToGB(xmlstring);
                xmlstring = s;
            }
            catch
            {
            }
            Log.Info("撤单/附加意见/催单");
            Log.Debug("接收数据：\r\n" + xmlstring);

            FilterHtmlCharts(ref xmlstring);

            string msg = "";
            try
            {
                var response = _workHepler.FollowUpOper(xmlstring);
                msg = response.ToString();
            }
            catch (Exception ex)
            {
                Log.Debug("接收数据错误：" + ex.Message);
            }
            Log.Debug("接收撤单/附加意见/催单完成");
            return msg;
        }

        /// <summary>
        /// 处理意见
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        [WebMethod]
        public string HandleInfo(string xmlstring)
        {
            try
            {
                string s = UnicodeToGB(xmlstring);
                xmlstring = s;
            }
            catch
            {
            }
            Log.Info("处理意见");
            Log.Debug("处理意见：\r\n" + xmlstring);

            FilterHtmlCharts(ref xmlstring);

            string msg = "";
            try
            {
                var response = _workHepler.HandleInfo(xmlstring);
                msg = response.ToString();
            }
            catch (Exception ex)
            {
                Log.Debug("接收数据错误：" + ex.Message);
            }
            Log.Debug("接收处理意见完成");
            return msg;
        }

        /// <summary>
        /// 回访确认
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        [WebMethod]
        [TraceExtension(Filename = "C:\\log\\RevisitNotice.log", Priority = 0)]
        public string RevisitNotice(string xmlstring)
        {
            try
            {
                string s = UnicodeToGB(xmlstring);
                xmlstring = s;
            }
            catch
            {
            }
            Log.Info("回访确认");
            Log.Debug("接收数据：\r\n" + xmlstring);

            var response = _workHepler.RevisitNotice(xmlstring);
            return response.ToString();
        }

        /// <summary>
        /// 审批挂起申请信息
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        [WebMethod]
        [TraceExtension(Filename = "C:\\log\\CheckHang.log", Priority = 0)]
        public string CheckHang(string xmlstring)
        {
            try
            {
                string s = UnicodeToGB(xmlstring);
                xmlstring = s;
            }
            catch
            {
            }
            Log.Info("审批挂起申请信息");
            Log.Debug("接收数据：\r\n" + xmlstring);

            var response = _workHepler.CheckHang(xmlstring);
            return response.ToString();
        }

        private void InitializeComponent()
        {

        }

        /// <summary>
        /// 将汉字转换为Unicode
        /// </summary>
        /// <param name="text">要转换的字符串</param>
        /// <returns></returns>
        public static string GBToUnicode(string text)
        {
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
            string lowCode = "", temp = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i % 2 == 0)
                {
                    temp = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容（两位16进制）
                    if (temp.Length < 2) temp = "0" + temp;
                }
                else
                {
                    string mytemp = Convert.ToString(bytes[i], 16);
                    if (mytemp.Length < 2) mytemp = "0" + mytemp; lowCode = lowCode + @"\u" + mytemp + temp;//取出元素4编码内容（两位16进制）
                }
            }
            return lowCode;
        }

        /// <summary>
        /// 将Unicode转换为汉字
        /// </summary>
        /// <param name="name">要转换的字符串</param>
        /// <returns></returns>
        public string UnicodeToGB(string text)
        {
            MatchCollection mc = Regex.Matches(text, "([\\w]+)|(\\\\u([\\w]{4}))");
            if (mc != null && mc.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Match m2 in mc)
                {
                    string v = m2.Value;
                    string word = v.Substring(2);
                    byte[] codes = new byte[2];
                    int code = Convert.ToInt32(word.Substring(0, 2), 16);
                    int code2 = Convert.ToInt32(word.Substring(2), 16);
                    codes[0] = (byte)code2;
                    codes[1] = (byte)code;
                    sb.Append(Encoding.Unicode.GetString(codes));
                }
                return sb.ToString();
            }
            else
            {
                return text;
            }
        }

        private void FilterHtmlCharts(ref string xmlstring)
        {
            if (string.IsNullOrEmpty(xmlstring))
                return;

            xmlstring = (new Regex(@"\&*nbsp\;*", RegexOptions.IgnoreCase)).Replace(xmlstring, string.Empty);
        }
    }
}
